package com.mxpio.erp.workshop.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.workshop.entity.*;
import com.mxpio.erp.common.workshop.service.ScreenReportService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ScreenReportServiceImpl implements ScreenReportService {
	@Override
	@Transactional
	public List<VWorkShopPassRateDetail> workShopPassRateMonthList() {//各车间生产质量情况
		Linq linq = JpaUtil.linq(VWorkShopPassRateDetail.class);
		Criteria criteria = Criteria.create();
		criteria.addCriterion("year", Operator.EQ, DateUtil.format(new Date(),"yyyy"));
		List<VWorkShopPassRateDetail> list = linq.aliasToBean(VWorkShopPassRateDetail.class).select("year", "mon", "workShopCode",
						linq.criteriaBuilder().sum(linq.root().get("cnt")).alias("cnt"),
						linq.criteriaBuilder().sum(linq.root().get("passCnt")).alias("passCnt"))
				.where(criteria)
				.groupBy("year", "mon", "workShopCode")
				.list();
		if (!list.isEmpty()){
			for (VWorkShopPassRateDetail detail:list){
				if (detail.getCnt()==null||detail.getCnt().compareTo(BigDecimal.ZERO)==0){
					detail.setPassRate(null);
				}else{
					detail.setPassRate(detail.getPassCnt().multiply(new BigDecimal("100")).divide(detail.getCnt(),2,BigDecimal.ROUND_HALF_UP));
				}
			}
		}
		return list;
	}

	@Override
	@Transactional
	public List<VWorkShopPassRateDetail> workShopPassYearList() {//生产入库不合格情况
		Linq linq = JpaUtil.linq(VWorkShopPassRateDetail.class);
		Criteria criteria = Criteria.create();
		criteria.addCriterion("yearmon", Operator.EQ, DateUtil.format(new Date(),"yyyy-MM"));
		List<VWorkShopPassRateDetail> list = linq.aliasToBean(VWorkShopPassRateDetail.class).select("yearmon",  "workShopCode",
						linq.criteriaBuilder().sum(linq.root().get("cnt")).alias("cnt"),
						linq.criteriaBuilder().sum(linq.root().get("passCnt")).alias("passCnt"))
				.where(criteria)
				.groupBy("yearmon", "workShopCode")
				.list();
		if (!list.isEmpty()){
			for (VWorkShopPassRateDetail detail:list){
				detail.setPassRate(detail.getPassCnt().multiply(new BigDecimal("100")).divide(detail.getCnt(),2,BigDecimal.ROUND_HALF_UP));
			}
		}
		return list;
	}

	@Override
	@Transactional
	public List<VPurchaseinInspectUnqualified> purchaseinUnqualifiedTop5() {//生产入库不合格情况
		Pageable pageAble = PageRequest.of(0, 5);
		Linq linq = JpaUtil.linq(VPurchaseinInspectUnqualified.class);
		Criteria criteria = Criteria.create();
		criteria.addCriterion("yearmon", Operator.EQ, DateUtil.format(new Date(),"yyyy-MM"));
		criteria.addOrder(new Order("failCnt",true));
		Page<VPurchaseinInspectUnqualified> paging = linq.where(criteria).paging(pageAble);
		return paging.getContent();
	}

	@Override
	@Transactional
	public List<VMOExecuteSum> moExecuteSum() {//统计生产当月(计划完成时间)执行情况，按车间汇总
		return  JpaUtil.linq(VMOExecuteSum.class).list();
	}

	@Override
	@Transactional
	public Page<VMODelay> moDelayTop50(Pageable pageAble) {//各车间延期订单信息 按延期天数降序，取前50条
		Linq linq = JpaUtil.linq(VMODelay.class);
		Criteria criteria = Criteria.create();
		criteria.addOrder(new Order("delayDays",true));
		return linq.where(criteria).paging(pageAble);
	}

	@Override
	@Transactional
	public List<VPODelay> poExecuteDelaySum() {//采购准时率 当月汇总
		return  JpaUtil.linq(VPODelay.class).list();
	}

	@Override
	@Transactional
	public Page<VPurchaseOrderLineExecDtail> purchaseOrderLineExecDelayList(Pageable pageAble) {//采购订单明细行执行情况详情
		Linq linq = JpaUtil.linq(VPurchaseOrderLineExecDtail.class);//suggest_arrive_date_
		Criteria criteria = Criteria.create();
		//时间范围取上个月26号到本月25号
		criteria.addCriterion("suggestArriveDate", Operator.GE, DateUtil.format(DateUtil.lastMonth(),"yyyy-MM")+"-26");
		criteria.addCriterion("suggestArriveDate", Operator.LE, DateUtil.format(new Date(),"yyyy-MM")+"-25");
		criteria.addCriterion("delayDays",Operator.GT,0);
		criteria.addOrder(new Order("delayDays",true));
		return linq.where(criteria).paging(pageAble);
	}

	@Override
	@Transactional
	public VMOCurMonFinishRate moCurMonFinishRate() {
		List<VMOCurMonFinishRate> list = JpaUtil.linq(VMOCurMonFinishRate.class).list();
		if (list.isEmpty()){
			return new VMOCurMonFinishRate();
		}else{
			return list.get(0);
		}
	}

	@Override
	@Transactional
	public List<VWorkShopCurMonFinishRate> workShopCurMonFinishRate() {
		return  JpaUtil.linq(VWorkShopCurMonFinishRate.class).list();
	}
}
